<template>
  <div>
    <el-card>
      <el-row>
        <el-col :span="20">
          <el-input v-model="searchModel.role_name" placeholder="角色名称" style="width: 180px" clearable></el-input>
          <el-button type="primary" round icon="el-icon-search" @click="search()">
            搜索
          </el-button>
        </el-col>
        <el-col :span="4" align="right">
          <el-button type="primary" icon="el-icon-plus" circle @click="editRoleForm(null)"></el-button>
        </el-col>
      </el-row>
    </el-card>

    <el-card>
      <el-table
        :data="roleList"
        stripe
        style="width: 100%">
        <el-table-column prop="roleId" label="角色ID" width="180"></el-table-column>
        <el-table-column prop="roleName" label="角色名称" width="180"></el-table-column>
        <el-table-column prop="roleDesc" label="描述"></el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" circle
                       @click="editRoleForm(scope.row.roleId)"></el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" circle
                       @click="deleteRoleById(scope.row.roleId)"></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="searchModel.pageNo"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="searchModel.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>

    <el-dialog @close="closeRoleForm()" :title="title" :visible.sync="dialogFormVisible">
      <el-form :model="roleForm" ref="userForm">
        <el-form-item label="角色名称" prop="roleName" label-width="180px">
          <el-input v-model="roleForm.roleName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="描述" label-width="180px">
          <el-input v-model="roleForm.roleDesc" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="权限设置" prop="menuIdList" label-width="180px"
        >
          <el-tree :data="menuList" :props="menuProps" show-checkbox default-expand-all
                   node-key="menuId" ref="menuRef"
          ></el-tree>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveRole()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import roleApi from "@/api/roleApi";
import menuApi from "@/api/menuApi";

export default {
  name: "role",
  data() {
    return {
      menuProps: {
        children: 'children',
        label: 'title'
      },
      menuList: [],
      roleList: [],
      roleForm: {},
      title: "",
      total: 1,
      dialogFormVisible: false,

      searchModel: {
        role_name: "",
        pageNo: 1,
        pageSize: 5,
      },
    }
  },
  methods: {
    closeRoleForm() {
      this.roleForm = {}
      this.$refs.menuRef.setCheckedKeys([])
    },
    saveRole() {
      let checkedKeys = this.$refs.menuRef.getCheckedKeys();
      let halfCheckedKeys = this.$refs.menuRef.getHalfCheckedKeys();
      this.roleForm.menuIds = halfCheckedKeys.concat(checkedKeys);
      roleApi.saveRole(this.roleForm).then(resp => {
        this.$message({
          message: resp.message,
          type: 'success'
        })
        this.dialogFormVisible = false;
        this.getRoleList()
      })
    },
    handleSizeChange(val) {
      this.searchModel.pageSize = val;
      this.getRoleList();
    },
    handleCurrentChange(val) {
      this.searchModel.pageNo = val;
      this.getRoleList();
    },
    search() {
      this.getRoleList()
    },
    editRoleForm(id) {
      if (id == null) {
        this.title = "新增角色"
        this.dialogFormVisible = true
      } else {
        roleApi.getRoleById(id).then(resp => {
          this.roleForm = resp.data.role;
          this.$refs.menuRef.setCheckedKeys(this.roleForm.menuIds)
        });
        this.title = "修改角色"
        this.dialogFormVisible = true;
      }
    },
    deleteRoleById(id) {
      this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(resp => {
        roleApi.deleteRoleById(id).then(resp => {
          this.$message({
            message: resp.message,
            type: 'success'
          })
          this.getRoleList()
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      })
    },
    getRoleList() {
      roleApi.getRoleList(this.searchModel).then(resp => {
        this.roleList = resp.data.rows;
        this.total = resp.data.total;
      })
    },
    getMenuList() {
      menuApi.getMenuList().then(resp => {
        this.menuList = resp.data;
      })
    }
  },
  created() {
    this.getRoleList()
    this.getMenuList();
  }
}
</script>

<style scoped>

</style>
